import * as uri from "@/config/uri";

/**
 * 问题推荐
 * @param focusNode 关注的节点
 * @param kg 节点所在的图谱
 */
export function questionRecommend(focusNode, kg) {
  focusNode = getNodeByUri(focusNode.uri, kg.nodes);
  if (focusNode === undefined) {
    console.log("questionRecommend", "node not exist");
    return new Set();
  }

  let typeNode = null;
  for (let i = 0; i < kg.properties.length; i++) {
    let propertie = kg.properties[i];
    if (propertie.uri === uri.RDF_TYPE && propertie.sourceId === focusNode.id) {
      typeNode = getNodeById(propertie.targetId, kg.types);
      break;
    }
  }

  if (typeNode === undefined) {
    console.log("questionRecommend", "type not exist");
    return new Set();
  }

  let resultSet = new Set(); //承接问题
  for (let i = 0; i < kg.properties.length; i++) {
    let propertie = kg.properties[i];
    console.log(resultSet);
    let sourceNode = getNodeById(propertie.sourceId, kg.nodes);
    let targetNode = getNodeById(propertie.targetId, kg.nodes);
    //获取domain关系答案
    if (
      propertie.uri === uri.RDFS_DOMAIN &&
      propertie.sourceId === typeNode.id
    ) {
      let question =
        focusNode.label_ZH + "的" + targetNode.label_ZH + "是什么?";
      resultSet.add(question);
    } else if (
      propertie.uri === uri.RDFS_RANGE &&
      propertie.targetId === typeNode.id
    ) {
      let question =
        "什么的" + sourceNode.label_ZH + "是" + focusNode.label_ZH + "?";
      resultSet.add(question);
    } else if (
      propertie.uri === uri.RDFS_DOMAIN &&
      propertie.targetId === typeNode.id
    ) {
      let question =
        focusNode.label_ZH + "的" + sourceNode.label_ZH + "是什么?";
      resultSet.add(question);
    } else if (
      propertie.uri === uri.RDFS_RANGE &&
      propertie.sourceId === typeNode.id
    ) {
      let question =
        "什么的" + targetNode.label_ZH + "是" + focusNode.label_ZH + "?";
      resultSet.add(question);
    }
  }
  return resultSet;
}

/**
 * 根据id获取节点
 * @param id
 * @param arr
 */
export function getNodeById(id, arr) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].id === id) {
      return arr[i];
    }
  }
}

export function getNodeByUri(uri, arr) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].uri === uri) {
      return arr[i];
    }
  }
}
